package com.huike.clues.utils.easyExcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.huike.clues.domain.vo.TbClueExcelVo;
import com.huike.clues.service.ITbClueService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * EasyExcel监听器，用于解析数据并执行操作
 */
public class ExcelListener extends AnalysisEventListener<TbClueExcelVo> {

	private Map<String,Integer> map = new ConcurrentHashMap<String,Integer> ();

	//每过5条插入一次
	private static final int BATCH_COUNT = 10;

	private Integer rowCount=0;

	public ITbClueService clueService;

	public ExcelListener(ITbClueService clueService) {
		this.clueService = clueService;
	}


	@Override
	public void invoke(TbClueExcelVo data, AnalysisContext context) {
		List<TbClueExcelVo> list = new ArrayList<TbClueExcelVo> ();
		list.add(data);
		//如果校验通过则添加数据入库
			Map<String, Integer> addTbClue = clueService.addTbClue(list);
			addMapData(addTbClue);
	}

	private void addMapData(Map<String, Integer> addTbClue) {
		Set<String> keySet = addTbClue.keySet();
		for (String indexKey : keySet) {
			Integer totalValue = map.get(indexKey);
			if(totalValue == null) {
				totalValue = 0;
			}
			Integer thisValue = addTbClue.get(indexKey);
			if(thisValue == null) {
				thisValue = 0;
			}
			map.put(indexKey, totalValue+thisValue);
		}
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
	}

	public Map<String,Integer> getResultData(){
		return map;
	}
}